package cn.dankal.flyaudio.login;

import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.HashMap;
import java.util.Map;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.DankalApplication;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.pojo.UserResponseBody;
import cn.dankal.basiclib.protocol.LoginProtocol;
import cn.dankal.basiclib.protocol.MainProtocol;
import cn.dankal.basiclib.protocol.PersonalProtocol;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.util.ViewUtils;
import cn.dankal.business.Constants;
import cn.dankal.flyaudio.R;

import static cn.dankal.business.Constants.Chinese;
import static cn.dankal.business.Constants.LANGUAGE;
import static cn.dankal.business.Constants.UUID;

/**
 * desc: 重置密码，通过验证码重置密码
 */
@Route(path = LoginProtocol.SET_LOGIN_PASSWD)
public class SetLoginPasswdActivity extends BaseActivity {

    public static final String MOBLIE = "mobile";
    public static final String CODE = "code";
    //验证验证码返回的key
    public static final String KEY = "key";
    @BindView(R.id.et_passwd)
    EditText mEtPasswd;
    @BindView(R.id.et_confirmpasswd)
    EditText mEtConfirmpasswd;

    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_loginpasswd;
    }

    @Override
    protected void initComponents() {

        type = getIntent().getStringExtra(Constants.TYPE);
        addSingleTitleBar(type);
    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String passWd = mEtPasswd.getText().toString();
        String confirmPasswd = mEtConfirmpasswd.getText().toString();
        if (!StringUtil.checkPasswd(passWd) || passWd.length() < 6) {
            ToastUtils.showShort("请输入6-20数字、字母、符号，不允许特殊符号`'\\“ 空格");
            return;
        }

        if (!StringUtil.checkPasswd(confirmPasswd) || confirmPasswd.length() < 6) {
            ToastUtils.showShort("请输入6-20数字、字母、符号，不允许特殊符号`'\\“ 空格");
            return;
        }

        if (!passWd.equals(confirmPasswd)) {
            ToastUtils.showShort("密码不一致");
            return;
        }

        String mobile = getIntent().getStringExtra(MOBLIE);
        String code = getIntent().getStringExtra(CODE);
        if (type.equals(Constants.TYPE_FORGET_PASS)) {
            String verificationCode = getIntent().getStringExtra(Constants.CODE);
            setPaseword(verificationCode, mobile, passWd);
        } else if (type.equals(Constants.TYPE_REGISTER)) {
            if (DankalApplication.openId != null) {
                wxLogin(mobile, passWd,code);
            } else register(mobile, passWd,code);
        }
    }

    /**
     * 设置密码
     *
     * @param code
     * @param mobile
     * @param passWd
     */
    private void setPaseword(String code, String mobile, String passWd) {
        Map<String, String> param = new HashMap();
        param.put("code", code);
        param.put("password", passWd);
        param.put("phone", mobile);
        param.put("rePassword", passWd);
        UserServiceFactory.reset_password(param).subscribe(new AbstractDialogSubscriber<UserInfoBean>(this) {
            @Override
            public void onNext(UserInfoBean o) {
                Toast.makeText(SetLoginPasswdActivity.this,"密码更改成功",Toast.LENGTH_SHORT).show();
                ARouter.getInstance().build(LoginProtocol.LOGIN).navigation();
                finish();
            }
        });
    }

    private void register(String mobile, String passWd,String code) {
        Map<String, String> map = new HashMap(6);
        map.put("userName", mobile);
        map.put("code",code);
        map.put("phone", mobile);
        map.put("password", passWd);
        map.put("rePassword", passWd);
        map.put(LANGUAGE, Constants.Chinese);
        map.put("id", getIntent().getStringExtra(UUID));

        UserServiceFactory.submitRegistered(map)
                .subscribe(new AbstractDialogSubscriber<UserInfoBean>(this) {
                    @Override
                    public void onNext(UserInfoBean userInfoBean) {
                        ToastUtils.showShort("密码设置成功");
                        ViewUtils.showVertifyDialog(SetLoginPasswdActivity.this, () -> {
                            ARouter.getInstance().build(PersonalProtocol.VERIFY).navigation();
                            finish();
                        });
                    }
                });
    }

    private void wxLogin(String mobile, String passWd,String code) {
        Map<String, String> maps = new HashMap<>(8);
        maps.put("accessToken", DKUserManager.getUserToken().token);
        maps.put("userName", mobile);
        maps.put("code", code);
        maps.put("phone", mobile);
        maps.put("password", passWd);
        maps.put("rePassword", passWd);
        maps.put("openId", DankalApplication.openId);
        maps.put(LANGUAGE, Chinese);
        maps.put("id", getIntent().getStringExtra(UUID));

       /* map.put("accessToken", "19 _cCLUUKC5ZMa52FufhX77NPDKWSzlVpOm - j035XWEuf - H3XXP8IaYtLSc_3hdWR1Ya0LO - 6 mWQK8DyUAjYqt4OI3TDEwDgT4FzqujMTYiheQ");
        map.put("userName", "17673139387");
        map.put("phone", "17673139387");
        map.put("password", "123456");
        map.put("rePassword", "123456");
        map.put("openId", "o7CVU1sc4aZXs8p - o - wTdO8I8A_0");
        map.put(LANGUAGE, Constants.Chinese);
        map.put("id", getIntent().getStringExtra(UUID));*/

        UserServiceFactory.wxLogin(maps).subscribe(new AbstractSubscriber<UserInfoBean>(this) {
            @Override
            public void onNext(UserInfoBean userInfoBean) {
                UserResponseBody userResponseBody = new UserResponseBody();
                UserResponseBody.TokenBean tokenBean = new UserResponseBody.TokenBean();
                tokenBean.token = userInfoBean.getAccessToken();
                userResponseBody.setToken(tokenBean);
                DKUserManager.saveUserInfo(userResponseBody);
                DKUserManager.updateUserInfo(userInfoBean);
                ARouter.getInstance().build(MainProtocol.MAIN).navigation();
                DankalApplication.openId = null;
                finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtils.showShort(e.getMessage());
            }
        });
    }
}
